package com.ytjj.qmyx.admin.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.admin.dao.MessageTelDao;
import com.ytjj.qmyx.admin.dao.OrdersDao;
import com.ytjj.qmyx.admin.service.MessageTelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 *
 * @Version: 1.0
 */
@Service
@Slf4j
public class MessageTelServiceImpl implements MessageTelService {

	@Resource
	private MessageTelDao messageTelDao;

	@Resource
	private OrdersDao ordersDao;

	@Override
	public void insertMessageTel() {
		List<String> telList = ordersDao.getTelList();
		if (!CollectionUtils.isEmpty(telList)) {
			for(String tel : telList) {
				// 运营商 1-移动 2-电信 3-联通 4-未知/其他
				Integer operatorType = 4;
				String operator = getTelInfo(tel);
				if(operator != null) {
					if(operator.equals("中国移动")) {
						operatorType = 1;
					} else if(operator.equals("中国电信")) {
						operatorType = 2;
					} else if(operator.equals("中国联通")) {
						operatorType = 3;
					}
					messageTelDao.insertTel(tel, operatorType);
				}
			}
		}

	}

	@Override
	public void insertUserTel() {
		long start = System.currentTimeMillis();
		// 开启线程池
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
				60L, TimeUnit.SECONDS,
				new SynchronousQueue<Runnable>());
		Integer pageSize = 1000;
		Integer pageNum = 1;
		boolean key = true;
		while (key) {
			PageHelper.startPage(pageNum, pageSize);
			List<String> telList = ordersDao.getTelList();
			if(telList.size() == pageSize) {
				pageNum++;
			} else {
				key = false;
			}
			// 执行线程
			threadPoolExecutor.execute(new insertUserTelHandler(telList));
		}
	}
	class insertUserTelHandler implements Runnable {

		private List<String> telephoneList;

		public insertUserTelHandler(List<String> telList) {
			this.telephoneList = telList;
		}

		@Override
		public void run() {
			if (!CollectionUtils.isEmpty(telephoneList)) {
					telephoneList.parallelStream().forEach(item -> {
					// 运营商 1-移动 2-电信 3-联通 4-未知/其他
					Integer operatorType = 4;
					String operator = getTelInfo(item);
					if(operator != null) {
						if(operator.equals("中国移动")) {
							operatorType = 1;
						} else if(operator.equals("中国电信")) {
							operatorType = 2;
						} else if(operator.equals("中国联通")) {
							operatorType = 3;
						}
						messageTelDao.insertTel(item, operatorType);
					}
				});
			}
		}
	}

	public String getTelInfo(String tel) {
		String telHttp = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + tel;
		String result = HttpRequest.get(telHttp)
				.header("Content-Type", "application/json; charset=UTF-8")
				.execute().body();
		String lastResult = result.replaceFirst("__GetZoneResult_ = ", "");
		JSONObject object = JSONObject.parseObject(lastResult);
		return object.getString("catName");
	}
}
